﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using com.ReinforceLab.AudioModem;
using com.ReinforceLab.AudioModem.FSK;

namespace WpfApplication1
{
    /// <summary>
    /// MortorControlWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MortorControlWindow : Window
    {
        const int baudRate = 1200;
        FSKModem _modem;
        packetSource _source;
        IAudioPHY _phy;

        public MortorControlWindow()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MortorControlWindow_Loaded);
            Unloaded += new RoutedEventHandler(MortorControlWindow_Unloaded);
        }

        void MortorControlWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            _modem.Stop();            
        }

        void MortorControlWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _startButton.IsEnabled = true;
            _stopButton.IsEnabled = false;

            _source = new packetSource();
            _modem = new FSKModem(BaudRate.B1200, _source);
            _phy = new OpenALSocket(_modem.SamplingRate);
            _modem.Start(_phy);            
        }        
        private void _startButton_Click(object sender, RoutedEventArgs e)
        {
            _startButton.IsEnabled = false;
            _stopButton.IsEnabled = true;

            _source.Start();
        }
        private void _stopButton_Click(object sender, RoutedEventArgs e)
        {
            _startButton.IsEnabled = true;
            _stopButton.IsEnabled = false;

            _source.Stop();
        }

        private void _leftMortorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            _leftPowerLable.Content = _leftMortorSlider.Value.ToString();
            _source.SetLeftPower((int)_leftMortorSlider.Value);
        }
        private void _rightMortorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            _rightPowerLabel.Content = _rightMortorSlider.Value.ToString();
            _source.SetRightPower((int)_rightMortorSlider.Value);
        }

        #region IPacketSource
        internal class packetSource : IPacketSource
        {
            int _leftPower;
            int _rightPower;
            bool _isRunning;

            #region Constructor
            public packetSource()
            {
                _isRunning = false;
                _leftPower = 0;
                _rightPower = 0;
            }
            #endregion

            #region Public methods
            public void SetLeftPower(int power)
            {
                _leftPower = power;
            }
            public void SetRightPower(int power)
            {
                _rightPower = power;
            }
            public void Start()
            {
                _isRunning = true;
            }
            public void Stop()
            {
                _isRunning = false;
            }
            #endregion

            #region IPacketSource メンバ
            public void ReceivePacket(byte[] packet, int validLength)
            {                
            }
            public byte[] SendPacket(out int validLength)
            {
                if (!_isRunning)
                {                    
                    validLength = 0;
                    return new byte[] { };
                }
                // build packet
                List<byte> packet = new List<byte>();
                packet.Add(0xE0);
                packet.Add(9);
                packet.Add((byte)_leftPower);
                packet.Add(0xE0);
                packet.Add(10);
                packet.Add((byte)_rightPower);
                
                validLength = packet.Count;
                return packet.ToArray();
            }
            #endregion
        }
        #endregion


    }
}
